package com.petalsgroup.petals.web.servlet.admin;

import com.petalsgroup.petals.pojo.*;
import com.petalsgroup.petals.service.admin.AdminService;
import com.petalsgroup.petals.service.admin.impl.AdminServiceIpml;
import com.petalsgroup.petals.service.animation.AnimationService;
import com.petalsgroup.petals.service.animation.impl.AnimationServiceImpl;
import com.petalsgroup.petals.util.LimitUtil;
import com.petalsgroup.petals.web.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;

@WebServlet("/admin/*")
public class AdminLoginServlet extends BaseServlet {
    AdminService as = new AdminServiceIpml();
    private AnimationService animationService = new AnimationServiceImpl();

    /**
     * 转到登陆界面
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void toLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/admin_view/login.jsp").forward(req, resp);
    }

    public void toAddAnimation(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/admin_view/addAnimation.jsp").forward(req, resp);
    }


    public void toUpdateAnimation(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/admin_view/updateAnimation.jsp").forward(req, resp);
    }

    /**
     * 尝试获取已经登陆的用户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void find(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object admin = req.getSession().getAttribute("loginAdmin");
        writeValue(resp,admin);
    }

    /**
     * 退出登录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void exit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().removeAttribute("loginAdmin");
        resp.sendRedirect(req.getContextPath()+"/admin/toLogin");
    }

    /**
     * 转跳到userAdmin界面（调用了userList）
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void userAdmin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("barid","userAdmin");
        req.getRequestDispatcher("/userAdmin/userList").forward(req,resp);
        //req.getRequestDispatcher("/WEB-INF/admin_view/userAdmin.jsp").forward(req, resp);
    }

    /**
     * 转跳到commentAdmin界面（调用了commentList）
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void commentAdmin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("barid","commentAdmin");
        req.getRequestDispatcher("/commentAdmin/commentList").forward(req,resp);
        //req.getRequestDispatcher("/WEB-INF/admin_view/commentAdmin.jsp").forward(req, resp);
    }


    /**
     * 转跳到animationAdmin界面（调用了animationList）
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void animationAdmin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("barid","animationAdmin");
        req.getRequestDispatcher("/admin/animationList").forward(req,resp);
        //req.getRequestDispatcher("/WEB-INF/admin_view/userAdmin.jsp").forward(req, resp);
    }



    /**
     * 管理员登录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void adminLogin(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
        req.setCharacterEncoding("utf-8");
        AdministratorDO admin = new AdministratorDO();
        try {
            BeanUtils.populate(admin,req.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        ResultInfo info = new ResultInfo();

        AdministratorDO loginAdmin = as.login(admin);
        Integer id=0;
        if (req.getSession().getAttribute("loginId")!=null){
            id = (Integer)req.getSession().getAttribute("loginId")+1;
        }else {
            req.getSession().setAttribute("loginId",1);
        }
        if (loginAdmin!=null){
            req.getSession().setAttribute("loginAdmin",loginAdmin);
            info.setFlag(true);
        }else if (loginAdmin == null && id<3){
            req.getSession().setAttribute("loginId",id);
            info.setFlag(false);
            info.setErrorMsg("账号或密码错误！");
        }else {
            info.setFlag(false);
            info.setErrorMsg("请勿非法登陆！");
        }
        writeValue(resp,info);
    }

    /**
     * 获取动漫信息list
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void animationList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //2.调用service中的方法 - 获取所有的用户信息
        List<UserDO> list = as.findAll();
        //3.存储用户信息 - request域中
        req.setAttribute("list",list);
        //4.转发到list.jsp界面显示
        req.getRequestDispatcher("/WEB-INF/admin_view/animationAdmin.jsp").forward(req, resp);
    }

    /**
     * 显示所有动漫
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author zqm
     */
    public void getAnim(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        LimitUtil limit = new LimitUtil();
        String total = req.getParameter("total");
        String num = req.getParameter("num");
        String currentPage = req.getParameter("currentPage");
        String search = req.getParameter("search");
        if (total != null && (!total.equals(""))) {
            limit.init(Long.valueOf(total), Integer.valueOf(num), Integer.valueOf(currentPage));
        }
        List<AnimationDetailedVO> list = animationService.listAnimationDetailed(limit, search);
        ResultInfo info = new ResultInfo();
        if (list == null) {
            info.setFlag(false);
            info.setErrorMsg("没有搜索到相关信息");
            writeValue(resp, info);
        } else {
            info.setFlag(true);
            HashMap<String, Object> map = new HashMap<>();
            map.put("limit", limit);
            map.put("list", list);
            info.setData(map);
            writeValue(resp, info);
        }

    }

    /**
     * 获取修改对象的信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author zqm
     */
    public void getUpdateAnim(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String parameter = req.getParameter("aid");
        int aid = Integer.parseInt(parameter);
        AnimationDetailedVO anim = animationService.getAnimationDetailed(aid);
        ResultInfo info = new ResultInfo();
        info.setData(anim);
        writeValue(resp, info);
    }

}
